import originAxios from 'axios'
import { message } from 'antd'

const axios = originAxios.create({
    timeout: 20000
})

axios.interceptors.response.use(
    function (response) {
        if (response.data && response.data.flag === 1) {
            /*
                successful response:
                {"flag": 0, "data": ""}

                unsuccessful response:
                {"flag": 1, "msg": "server error"}
            */
            let errorMsg = response.data.msg
            message.error(errorMsg)
            return Promise.reject(errorMsg)
        }
        return response.data
    },
    function (error) {
        return Promise.reject(error)
    }
)

export function get(url: string, data: any) {
    return axios.get(url, {
        params: data
    })
}

// By default, axios serializes JavaScript objects to JSON.
export function post(url: string, data: any) {
    return axios({
        method: 'post',
        url,
        data
    })
}

export default axios
